'use strict';
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
 * Chaincode query & invoke
 */
var chainquery = require('./query');
var chaininvoke = require('./invoke');
var chaininvoke_1 = require('./invoke-1');
var aes = require('./aes');

// var Fabric_Client = require('fabric-client');
// var path = require('path');
// var util = require('util');
// var os = require('os');
// var fs = require('fs');
// var http = require('http');
var express = require('express');
// let co = require("co");
var app = express();
var option = [];
var option1 = [];

// Nodejs encryption with CTR
// var crypto = require('crypto'),
//     algorithm = 'aes-256-ctr',
//     password = 'd6F3Efeq';

// function encrypt(text){
//   var cipher = crypto.createCipher(algorithm,password)
//   var crypted = cipher.update(text,'utf8','hex')
//   crypted += cipher.final('hex');
//   return crypted;
// }
 
// function decrypt(text){
//   var decipher = crypto.createDecipher(algorithm,password)
//   var dec = decipher.update(text,'hex','utf8')
//   dec += decipher.final('utf8');
//   return dec;
// }

//  主页输出 "info"
app.get('/query', function (req, res) {
// app.get('/query', function (req, res) {
   console.log("参数传递:");
   var CAR_ID = req.query.id;
   var fcn = req.query.fcn;
   option[0] = CAR_ID; 
   option[1] = fcn;

   // console.log(opt, queryCar);
   console.log(option);

   var result;
   result = chainquery.query(option,res);
   
})

//  主页输出 "info"
app.get('/invoke', function (req, res) {
// app.get('/query', function (req, res) {
   console.log("参数传递:");
   var CAR_ID = req.query.id;
   var newowner = req.query.New;
   var price = req.query.Price;
   var org = req.query.Org;
   var kilometres = req.query.Kil;
   var accident = req.query.Acc;
   var security = req.query.Sec;
   var steering = req.query.Ste;
   var lighting_air =req.query.Lig;
   var power = req.query.Pow;
   var fcn = req.query.fcn;
   option1[0] = CAR_ID; 
   // option1[1] = newowner;
   // option1[1] = md5.update(newowner).digest('hex');
   //加密
   // option1[1] = aes.encrypt(newowner);
   option1[1] = newowner;
   option1[2] = price;
   option1[3] = org;
   option1[4] = kilometres;
   option1[5] = accident;
   option1[6] = security;
   option1[7] = steering;
   option1[8] = lighting_air;
   option1[9] = power;
   option1[10] = fcn;

   console.log('加密:', option1);

   //解密
   // option1[1] = decrypt(option1[1]);

   // console.log(opt, queryCar);
   // console.log('解密:',option1);

   var result;
   result = chaininvoke_1.invoke(option1,res);
   
})

var server = app.listen(8081, function () {
 
  var host = server.address().address
  var port = server.address().port
 
  console.log("应用实例，访问地址为 http://%s:%s", host, port)
 
})
